package com.yuven.baselib.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.hjq.toast.ToastUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 网络环境监测工具
 * 1.是否有网络 {@link NetHelper#isNetAvailable()}
 * 3.获取当前网络类型 {@link NetHelper#getNetworkType()}
 * 4.当前是否是WIFI状态 {@link NetHelper#isNetWifi()}
 */
public class NetHelper {

    public static final int NET_NONE = 0;//没有网络
    public static final int NET_2G = 2;
    public static final int NET_3G = 3;
    public static final int NET_4G = 4;
    public static final int NET_5G = 5;
    public static final int NET_LINE = 6;
    public static final int NET_WIFI = 1;
    // 适配低版本手机
    public static final int NETWORK_TYPE_NONE = -1;//没有网络
    public static final int NETWORK_TYPE_WIFI = -101;
    public static final int NETWORK_TYPE_GPRS = 1;
    public static final int NETWORK_TYPE_EDGE = 2;
    public static final int NETWORK_TYPE_UMTS = 3;
    public static final int NETWORK_TYPE_CDMA = 4;
    public static final int NETWORK_TYPE_EVDO_0 = 5;
    public static final int NETWORK_TYPE_EVDO_A = 6;
    public static final int NETWORK_TYPE_1xRTT = 7;
    public static final int NETWORK_TYPE_HSDPA = 8;
    public static final int NETWORK_TYPE_HSUPA = 9;
    public static final int NETWORK_TYPE_HSPA = 10;
    public static final int NETWORK_TYPE_IDEN = 11;
    public static final int NETWORK_TYPE_EVDO_B = 12;
    public static final int NETWORK_TYPE_LTE = 13;
    public static final int NETWORK_TYPE_EHRPD = 14;
    public static final int NETWORK_TYPE_HSPAP = 15;
    public static final int NETWORK_TYPE_NR = 20;

    /**
     * 是否网络OK
     *
     * @return
     */
    public static boolean isNetAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) ToolsKit.getClient()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isAvailable() && info.isConnected()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否网络OK
     *
     * @return
     */
    public static boolean checkNetToast() {
        boolean isNet = isNetAvailable();
        if (!isNet) {
            ToastUtils.show("网络不给力！");
        }
        return isNet;
    }

    /**
     * 是否是WIFI
     *
     * @return
     */
    public static boolean isNetWifi() {
        return getNetworkType() == NET_WIFI;
    }



    /**
     * 获取网络类型
     * 网络类型：6、WiFi；2、2G；3、3G；4、4G；7、有线网络
     *
     * @return
     */
    public static int getNetworkType() {
        int networkType = 0;
        NetworkInfo info = ((ConnectivityManager) ToolsKit.getClient()
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            int type = info.getType();
            if (type == ConnectivityManager.TYPE_WIFI) {
                networkType = NET_WIFI;
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                String subtypeName = info.getSubtypeName();
                int subType = info.getSubtype();
                switch (subType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        networkType = NET_2G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                        networkType = NET_3G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                        networkType = NET_4G;
                        break;
                    case NETWORK_TYPE_NR:
                        networkType = NET_5G;
                    default:
                        // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                        if (subtypeName.equalsIgnoreCase("TD-SCDMA") ||
                                subtypeName.equalsIgnoreCase("WCDMA") ||
                                subtypeName.equalsIgnoreCase("CDMA2000")) {
                            networkType = NET_3G;
                        }
                        break;
                }
            } else {
                networkType = NET_LINE;
            }
        }

        return networkType;
    }


    /**
     * 获取网络描述
     *
     * @param net
     * @return
     */
    public static String getNetDescription(int net) {
        switch (net) {
            case NET_LINE:
                return "wired network";
            case NET_WIFI:
                return "WiFi";
            case NET_5G:
                return "5G";
            case NET_4G:
                return "4G";
            case NET_3G:
                return "3G";
            case NET_2G:
                return "2G";
            case NET_NONE:
                return "no network";
            default:
                return "unknow";
        }
    }

    /**
     * 获取内置的网络类型判断
     *
     * @return
     */
    private static int getInnerNetWorkType() {
        int network = NETWORK_TYPE_NONE;
        try {
            final NetworkInfo info = ((ConnectivityManager) ToolsKit.getClient()
                    .getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getActiveNetworkInfo();
            if (info != null && info.isAvailable() && info.isConnected()) {
                int type = info.getType();
                if (type == ConnectivityManager.TYPE_WIFI) {
                    network = NETWORK_TYPE_WIFI;
                } else if (type == ConnectivityManager.TYPE_MOBILE) {
                    TelephonyManager telephonyManager = (TelephonyManager) ToolsKit.getClient()
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    network = telephonyManager.getNetworkType();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return network;
    }


    /**
     * 获取当前系统连接网络的网卡的mac地址
     *
     * @return
     */
    public static final String getMac() {
        byte[] mac = null;
        StringBuffer sb = new StringBuffer();
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();

                while (address.hasMoreElements()) {
                    InetAddress ip = address.nextElement();
                    if (ip.isAnyLocalAddress() || !(ip instanceof Inet4Address) || ip.isLoopbackAddress())
                        continue;
                    if (ip.isSiteLocalAddress())
                        mac = ni.getHardwareAddress();
                    else if (!ip.isLinkLocalAddress()) {
                        mac = ni.getHardwareAddress();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }

        if (mac != null) {
            for (int i = 0; i < mac.length; i++) {
                sb.append(parseByte(mac[i]));
            }
            return sb.substring(0, sb.length() - 1);
        }
        return "";

    }

    private static String parseByte(byte b) {
        String s = "00" + Integer.toHexString(b) + ":";
        return s.substring(s.length() - 3);
    }

}
